home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / decwrl / mkdb.pl < prev    next >
Encoding:
Perl Script  |  1996-10-25  |  5.9 KB  |  264 lines

  1. #! /usr/bin/perl -w
  2. #
  3. # $Header: /proj/src/isc/cvs-1/bind/contrib/decwrl/mkdb.pl,v 8.2 1996/10/25 17:03:12 vixie Exp $
  4. #
  5. # from a hosts(5) database on standard input or argv,
  6. # build the .data and .zone files needed by named.boot
  7. #
  8. # $Log: mkdb.pl,v $
  9. # Revision 8.2  1996/10/25 17:03:12  vixie
  10. # BIND 4.9.5 T6B
  11. #
  12. % Revision 1.37  1993/09/08  00:00:31  vixie
  13. % ckp
  14. %
  15. #
  16.  
  17. #++ BEGIN user servicable parts 01/02
  18. $Domain = 'pa.dec.com';
  19. $Zone = 'decpa';
  20. %DelegOctet = (
  21.     "16", 2,        # net 16 delegates at the second octet in pa
  22.     );
  23. $Debug = 4;
  24. #-- END user servicable parts 01/02
  25.  
  26. $, = ' ';
  27. umask(022);
  28. chop($date = `/bin/date`);
  29. $Serial_File = 'version';
  30. $MX_File = 'mx_data';
  31. ($Domain_Pat = "$Domain") =~ s/(\W)/\\$1/go;
  32. $Domain_Pat = "\\.$Domain_Pat\$";
  33.  
  34. &Load_MX_Data();
  35. &Update_Serial_Number();
  36. &Update_Zone_Serials();    
  37. &Init_Zone_Datafiles();
  38. &Churn();
  39. &Close_Zone_Datafiles();
  40.  
  41. exit 0;
  42.  
  43. sub Find_Deleg {
  44.     local(@a) = @_;
  45.     local($x, $deleg);
  46.  
  47.     for ($x = 3;  $x >= 0;  $x--) {
  48.         $deleg = $DelegOctet{join('.', @a[0..$x])};
  49.         last if $deleg;
  50.     }
  51.     if ($x < 0) {
  52.         $deleg = 3;    # default for unspecified nets
  53.     }
  54.     @a = (    join('.', @a[0..($deleg-1)]),
  55.         join('.', reverse(@a[$deleg..3]))
  56.     );
  57.     print "Find_Deleg(@_) -> (@a)\n"        if $Debug & 0x01;
  58.     return @a;
  59. }
  60.  
  61. sub Write_RR {
  62.     local($fh, $name, $type, $data) = @_;
  63.  
  64.     print "Write_RR(@_)\n"                if $Debug & 0x02;
  65.     if ($name eq $Write_RR'last_name{$fh}) {
  66.         $name = '';
  67.     } else {
  68.         $Write_RR'last_name{$fh} = $name;
  69.     }
  70.     printf $fh "%-30s in %-5s %s\n", $name, $type, $data;
  71. }
  72.  
  73. sub Load_MX_Data {
  74.     open(f, "<$MX_File") || die "$MX_File: $!";
  75.     local($name) = ('');
  76.     while (<f>) {
  77.         chop;
  78.         s/\#.*$//;    # delete inline comments like this one
  79.         s/\s+$//;    # delete trailing whitespace
  80.         next unless length($_);
  81.         if (/^\s+/) {
  82.             $_ = $';    # default name
  83.         } elsif (/\s+/) {
  84.             $name = $`;    # specified name
  85.             $_ = $';
  86.         } else {
  87.             warn "$MX_File:$.: bad format ($_)\n";
  88.             next;
  89.         }
  90.         ($cost, $target, @rest) = split;
  91.         if ($cost <= 0 || $cost > 32767) {
  92.             warn "$MX_File:$.: bad MX cost ($cost)\n";
  93.             next;
  94.         }
  95.         if ($#rest >= $[) {
  96.             warn "$MX_File:$.: extra tokens ($_)\n";
  97.             next;
  98.         }
  99.         $MX_Data{$name} = '' unless defined($MX_Data{$name});
  100.         $MX_Data{$name} .= "$cost $target\n";
  101.     }
  102.     close(f);
  103. }
  104.  
  105. sub Write_MX_Data {
  106.     local($zone, $name) = @_;
  107.     local($n, $d, $_);
  108.  
  109.     print "Write_MX_Data(@_)\n"            if $Debug & 0x04;
  110.     foreach $n ($name, "*") {
  111.         foreach $d (split(/\n/, $MX_Data{$n})) {
  112.             &Write_RR($zone, $name, 'mx', $d);
  113.         }
  114.     }
  115. }
  116.  
  117. sub Update_Serial_Number {
  118.     local(*f);
  119.  
  120.     if (open(f, "<$Serial_File") && ($Serial_No = <f>)) {
  121.         close(f);
  122.     } else {
  123.         $Serial_No = 0;
  124.     }
  125.  
  126.     $Serial_No++;
  127.  
  128.     open(f, ">$Serial_File") || die "$Serial_File: $!";
  129.     print f "$Serial_No\n";
  130.     close(f);
  131.  
  132.     print "*** New serial number is $Serial_No\n";
  133. }
  134.  
  135. sub Update_Zone_Serials {
  136.     local(*f, $zonefile, $zone);
  137.  
  138.     foreach $zonefile (<*.zone>) {
  139.         $zone = $zonefile;
  140.         $zone =~ s/\.zone$//;
  141.         $Zones{$zone}++ unless ($zone eq $Zone);
  142.         print "*** updating zone file '$zonefile'\n";
  143.         open(f, "<$zonefile") || die "$zonefile: $!";
  144.         @f = <f>;
  145.         open(f, ">$zonefile.NEW") || die "$zonefile.NEW: $!";
  146.         foreach (@f) {
  147.             $_ = $`.$Serial_No.$1."\n" if (/\d+(\s+\;Serial)/);
  148.             print f;
  149.         }
  150.         close(f) || die "$zonefile.NEW: $!";
  151.     }
  152.  
  153.     print "*** Zone file serial numbers updated\n";
  154. }
  155.  
  156. sub Init_Zone_Datafiles {
  157.     #
  158.     # our name servers are considered authoritative for the following
  159.     # zones of in-addr.arpa.  we create a .data file for each here.  None
  160.     # of them have SOA's since we expect them to be $INCLUDE'd by .zone
  161.     # files.
  162.     #
  163.     local($zone);
  164.  
  165.     foreach $zone (sort(keys(%Zones))) {
  166.         print "*** Init'ing zone ".$zone."\n";
  167.         open($zone, ">$zone.data.NEW") || die "$zone.data.NEW: $!";
  168.         $rzone = join('.', reverse(split(/\./, $zone)))
  169.                . '.in-addr.arpa.';
  170.         print $zone "; $zone.data ($zone - $rzone) $date\n";
  171.         print $zone '$ORIGIN '.$rzone."\n";
  172.     }
  173.  
  174.     # the main host->addr file has no origin statement in it since
  175.     # its name is not mechanically translatable to its zone.
  176.     open($Zone, ">$Zone.data.NEW") || die "$Zone.data.NEW: $!";
  177.     &Write_MX_Data($Zone, "@");
  178. }
  179.  
  180. sub Close_Zone_Datafiles {
  181.     foreach ($Zone, keys(%Zones)) {
  182.         close($_) || die "$_.data.NEW: $!";
  183.         rename("$_.data", "$_.data.BAK");
  184.         rename("$_.data.NEW", "$_.data") || die "rename1 ($_): $!";
  185.         rename("$_.zone", "$_.zone.BAK");
  186.         rename("$_.zone.NEW", "$_.zone") || die "rename2 ($_): $!";
  187.     }
  188. }
  189.  
  190. sub Churn {
  191.     print "*** Churning\n";
  192. host:
  193.     while (<>) {
  194.     chop;
  195.  
  196.     # capture and convert comments
  197.     if (/^\s*\#/) {
  198.         $_ = ';' . $';
  199.         print $Zone "$_\n";
  200.         last host if (/BELOW HERE NOT REGISTERED WITH DOMAIN SERVER/);
  201.         next host;
  202.     }
  203.     s/\s*\#.*$//;
  204.  
  205.     # parse line, skip it if addr doesn't have four components
  206.     y/A-Z/a-z/;
  207.     ($a, $name, @aliases) = split;
  208.     next host if (4 != (@a = split(/\./, $a)));
  209.     $name =~ s/$Domain_Pat//o;
  210.  
  211.     #
  212.     # names that still have dots need to be qualified since they
  213.     # aren't local.
  214.     #
  215.     # only local names get MX RR's generated for them.
  216.     #
  217.     if ($name =~ /\./) {
  218.         $fullname = $name . '.';
  219.     } else {
  220.         &Write_RR($Zone, $name, 'a', $a);
  221.         #
  222.         # do not generate mx rr's for network or broadcast a rr's
  223.         # or for loopback addresses
  224.         #
  225.         if ($a[0] != 127 && $a[3] != 0 && $a[3] != 255) {
  226.             &Write_RR($Zone, $name, 'mx', "0 $name");
  227.             &Write_MX_Data($Zone, $name);
  228.         }
  229.         $fullname = $name . ".$Domain.";
  230.     }
  231.  
  232.     ($zone, $ptr) = &Find_Deleg(@a);
  233.  
  234.     #
  235.     # if this is a zone we support, write out a PTR for it
  236.     #
  237.     if ($Zones{$zone}) {
  238.         &Write_RR($zone, $ptr, 'ptr', $fullname);
  239.     }
  240.  
  241.     #
  242.     # iterate over the aliases, if any, and generate CNAMEs and PTRs.
  243.     #
  244.     foreach (@aliases) {
  245.         s/$Domain_Pat//o;
  246.  
  247.         # aliases that are the same as the basename aren't useful
  248.         next if ($_ eq $name);
  249.  
  250.         # CNAME makes sense only for local names
  251.         if (! /\./) {
  252.             $maybedot = '.' if ($name =~ /\./);
  253.             &Write_RR($Zone, $_, 'cname', $name.$maybedot);
  254.         }
  255.  
  256.         # PTR makes sense only for zones we support (NOTE: $_ smashed)
  257.         if ($Zones{$zone}) {
  258.             $_ .= ".$Domain" unless /\./;
  259.             &Write_RR($zone, $ptr, 'ptr', "$_.");
  260.         }
  261.     }
  262.     }
  263. }
  264.